package com.itao.jdk;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParserFactory;

/**
 * Sax只能读取xml
 * @Author sjt
 * @CreateTime 2023/10/15 13:08
 */
public class SaxTest {

    public static void main(String[] args) throws Exception{
        var factory = SAXParserFactory.newInstance();
        var saxParser = factory.newSAXParser();
        saxParser.parse("E:\\idea\\java-learn\\xml\\src\\main\\resources\\read.xml", new DefaultHandler(){
            @Override
            public void startDocument() throws SAXException {
                System.out.println("开始读取dom");
                super.startDocument();
            }

            @Override
            public void endDocument() throws SAXException {
                System.out.println("读取dom结束");
                super.endDocument();
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                System.out.print("<" + qName + ">");
                super.startElement(uri, localName, qName, attributes);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                System.out.println("</" + qName + ">");
                super.endElement(uri, localName, qName);
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                System.out.print(new String(ch, start, length));
                super.characters(ch, start, length);
            }
        });
    }

}
